package com.allqj.sfx.infrastructure.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sjw
 * @className SwaggerConfig
 * @date 2022/7/19 11:50
 * @description: swagger
 */

@Configuration
@EnableOpenApi
public class SwaggerConfig {

    private ApiInfo apiInfo(String apiTitle, String apiDesc, String apiVersion) {
        return new ApiInfoBuilder()
                .title(apiTitle)
                .description(apiDesc)
                .version(apiVersion)
                .build();
    }

    private Docket getDocket(String groupName, String apiTitle, String apiDesc, String apiVersion, String[] paths) {
        List<RequestParameter> requestParameterList = new ArrayList<>();
        RequestParameter requestParameter = new RequestParameterBuilder()
                .name("token")
                .description("用户Token")
                .in(ParameterType.HEADER)
                .required(false)
                .build();
        requestParameterList.add(requestParameter);

        ApiSelectorBuilder apiSelectorBuilder = new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo(apiTitle, apiDesc, apiVersion))
                .groupName(groupName)
                .select()
                .apis(RequestHandlerSelectors.any());
        if (paths == null) {
            apiSelectorBuilder.paths(PathSelectors.any());
        } else {
            StringBuilder pathRegex = new StringBuilder();
            for (String path : paths) {
                pathRegex.append("(").append(path).append(")|");
            }
            apiSelectorBuilder.paths(PathSelectors.regex(pathRegex.substring(0, pathRegex.length() - 1)));
        }
        return apiSelectorBuilder
                .build()
                .globalRequestParameters(requestParameterList);
    }

    @Bean
    public Docket docket() {
        return getDocket("default", "省房协", "会员管理&门户网站&实名认证系统", "v1", null);
    }

    @Bean
    public Docket member() {
        return getDocket("会员管理", "省房协-会员管理", "会员管理：房产协会会员管理系统", "v1",
                new String[]{"/v1/member/.*"});
    }

    @Bean
    public Docket webSite() {
        return getDocket("房协门户网站", "省房协-门户网站", "房协门户网站：房产资讯门户", "v1",
                new String[]{"/v1/website/.*"});
    }

    @Bean
    public Docket realName() {
        return getDocket("实名认证系统", "省房协-实名认证系统", "实名认证系统：房地产从业机构、人员实名认证系统", "v1",
                new String[]{"/v1/register/.*"});
    }
}
